package com.tedu.parkinglot.carLog.service.impl;

import com.tedu.parkinglot.base.constants.OrderConstant;
import com.tedu.parkinglot.base.exception.ServiceException;
import com.tedu.parkinglot.base.response.StatusCode;
import com.tedu.parkinglot.carLog.mapper.CarLogMapper;
import com.tedu.parkinglot.carLog.pojo.dto.CarLogQuery;
import com.tedu.parkinglot.carLog.pojo.dto.CarLogSaveParam;
import com.tedu.parkinglot.carLog.pojo.entity.Car;
import com.tedu.parkinglot.carLog.pojo.entity.CarLog;
import com.tedu.parkinglot.carLog.pojo.vo.CarLogVO;
import com.tedu.parkinglot.carLog.pojo.vo.ChargeVO;
import com.tedu.parkinglot.carLog.service.CarLogService;
import com.tedu.parkinglot.chargeRule.mapper.ChargeRuleMapper;
import com.tedu.parkinglot.chargeRule.pojo.dto.ChargeRuleQuery;
import com.tedu.parkinglot.chargeRule.pojo.vo.ChargeRuleVO;
import com.tedu.parkinglot.order.mapper.OrderMapper;
import com.tedu.parkinglot.order.pojo.dto.OrderSaveParam;
import com.tedu.parkinglot.order.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class CarLogServiceImpl implements CarLogService {

    @Autowired
    private CarLogMapper carLogMapper;
    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private ChargeRuleMapper chargeRuleMapper;

    @Override
    public List<CarLogVO> select(CarLogQuery carLogQuery) {
        log.info("查询车辆日志：{}", carLogQuery);
        List<CarLogVO> carLogVOList = carLogMapper.select(carLogQuery);
        log.info("查询车辆日志结果：{}", carLogVOList);
        return carLogVOList;
    }



    @Override
    public ChargeVO exit(Long id) {
        CarLogQuery carLogQuery = new CarLogQuery();
        carLogQuery.setId(id);
        log.debug("查询车辆日志：{}", carLogQuery);
        List<CarLogVO> list = carLogMapper.select(carLogQuery);
        Date entranceTime = list.get(0).getEntranceTime();
        CarLog carLog = new CarLog();
        carLog.setId(id);
//        carLog.setLeaveTime(LocalDateTime.now());
        //设置离开时间为当前时间"yyyy-MM-dd HH:mm:ss",并格式化为Date类型
        carLog.setLeaveTime(new Date());
        log.debug("车辆入场时间：{}", entranceTime);
        log.debug("车辆出场时间：{}", carLog.getLeaveTime());
        carLog.setUserTime((carLog.getLeaveTime().getTime() - entranceTime.getTime()) / 1000 / 60);
        log.debug("用户停车时间：{}分钟", carLog.getUserTime());
        log.debug("保存车辆停车日志：{}", carLog);
        carLogMapper.exit(carLog);
        // 订单表中更新车辆信息
        log.debug("开始更新订单表");
        OrderSaveParam orderSaveParam = new OrderSaveParam();
        orderSaveParam.setParkRecordId(id);
        orderSaveParam.setId(orderMapper.getOrderIdByParkRecordId(id));
        //获取计费规则
        ChargeRuleQuery chargeRuleQuery = new ChargeRuleQuery();
        chargeRuleQuery.setParkingId(list.get(0).getParkingId());
        log.debug("查询计费规则：{}", list.get(0));
        log.debug("查询计费规则：{}", chargeRuleQuery);
        List<ChargeRuleVO> chargeRuleVOS = chargeRuleMapper.select(chargeRuleQuery);
        if (chargeRuleVOS.size() > 0) {
            ChargeRuleVO chargeRuleVO = chargeRuleVOS.get(0);
            //停车天数：
            Long d= ((carLog.getUserTime()-chargeRuleVO.getFreetime())/60)/24;
            Long damt = d * chargeRuleVO.getFeeLimit();
            Long tamt = (((carLog.getUserTime()-chargeRuleVO.getFreetime())/60)+1)*chargeRuleVO.getPrice();
            if (tamt > chargeRuleVO.getFeeLimit()){
                tamt = chargeRuleVO.getFeeLimit();
            }
            orderSaveParam.setAmount(damt+tamt);
        } else {
            //orderSaveParam.setAmount(0L);
            throw new ServiceException(StatusCode.RULE_UNEXISTS);
        }
        orderService.saveOrder(orderSaveParam);
        log.debug("更新订单表成功");
        ChargeVO chargeVO = new ChargeVO();
        chargeVO.setParkRecordId(id);
        chargeVO.setAmount(orderSaveParam.getAmount());
        chargeVO.setEntranceTime(entranceTime);
        chargeVO.setLeaveTime(carLog.getLeaveTime());
        chargeVO.setUserTime(carLog.getUserTime());
        chargeVO.setOrderId(orderSaveParam.getId());
        return chargeVO;
    }

    @Override
    public void save(CarLogSaveParam param) {
        log.info("保存车辆日志：{}", param);
        CarLog carLog = new CarLog();
        Car car = new Car();
        car.setParkingId(param.getParkingId());
        car.setLicense(param.getLicense());
        carLogMapper.carSave(car);
        param.setCarId(car.getId());
        BeanUtils.copyProperties(param, carLog);
        carLog.setUserTime(0L);
        carLogMapper.save(carLog);
        log.info("保存车辆日志成功：{}", carLog);
        // 订单表中添加车辆信息
        log.debug("开始保存订单表");
        OrderSaveParam orderSaveParam = new OrderSaveParam();
        orderSaveParam.setCarId(car.getId());
        orderSaveParam.setParkingId(param.getParkingId());
        orderSaveParam.setOrderType(OrderConstant.ORDER_TYPE_PARKCHARGE);
        orderSaveParam.setParkRecordId(carLog.getId());
        orderService.saveOrder(orderSaveParam);
    }

}
